package com.haohan.jingyuan.customer.helper;

import com.haohan.jingyuan.customer.dto.resp.CustomerFileResp;
import com.haohan.jingyuan.customer.entity.CustomerFile;
import com.haohan.jingyuan.customer.entity.CustomerGroupInfo;
import com.haohan.jingyuan.customer.entity.CustomerTag;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 客户材料响应对象转换工具类
 *
 * @ClassName CustomerFileRespHelper
 * @Description 客户材料实体与响应对象转换工具
 * @Author hexijun
 * @Time 2025/01/31 10:00
 * @Version 1.0
 */
public class CustomerFileRespHelper {

    /**
     * 将CustomerFile实体转换为CustomerFileResp响应对象
     *
     * @param customerFile 客户材料实体
     * @return 客户材料响应对象
     */
    public static CustomerFileResp convert2CustomerFileResp(CustomerFile customerFile) {
        return convert2CustomerFileResp(customerFile, null, null);
    }

    /**
     * 将CustomerFile实体转换为CustomerFileResp响应对象（带分组和标签名称）
     *
     * @param customerFile 客户材料实体
     * @param groupMap 分组信息Map，key为groupId，value为CustomerGroupInfo
     * @param tagMap 标签信息Map，key为tagCode，value为CustomerTag
     * @return 客户材料响应对象
     */
    public static CustomerFileResp convert2CustomerFileResp(CustomerFile customerFile,
                                                            Map<String, CustomerGroupInfo> groupMap,
                                                            Map<String, CustomerTag> tagMap) {
        if (customerFile == null) {
            return null;
        }

        CustomerFileResp resp = new CustomerFileResp();
        resp.setId(customerFile.getId());
        resp.setCustomerId(customerFile.getCustomerId());
        resp.setFileKey(customerFile.getFileKey());
        resp.setFileName(customerFile.getFileName());
        resp.setFileType(customerFile.getFileType());
        resp.setSignAt(customerFile.getSignAt());
        resp.setExpireAt(customerFile.getExpireAt());
        resp.setGroupCode(customerFile.getGroupCode());
        resp.setTag(customerFile.getTag());
        resp.setUkid(customerFile.getUkid());
        resp.setCreator(customerFile.getCreator());
        resp.setGmtCreated(customerFile.getGmtCreated());
        resp.setModifier(customerFile.getModifier());
        resp.setGmtModified(customerFile.getGmtModified());
        resp.setIsDeleted(customerFile.getIsDeleted());

        // 设置分组名称
        if (groupMap != null && customerFile.getGroupCode() != null && !customerFile.getGroupCode().trim().isEmpty()) {
            CustomerGroupInfo groupInfo = groupMap.get(customerFile.getGroupCode());
            if (groupInfo != null) {
                resp.setGroupName(groupInfo.getGroupName());
            }
        }

        // 设置标签名称（支持多个标签，逗号分隔）
        if (tagMap != null && customerFile.getTag() != null && !customerFile.getTag().trim().isEmpty()) {
            String[] tagCodes = customerFile.getTag().split(",");
            Set<String> tagNames = Stream.of(tagCodes)
                    .map(String::trim)
                    .filter(code -> !code.isEmpty())
                    .map(code -> tagMap.get(code))
                    .filter(tag -> tag != null)
                    .map(CustomerTag::getTagName)
                    .collect(Collectors.toSet());
            if (!tagNames.isEmpty()) {
                resp.setTagName(String.join(",", tagNames));
            }
        }

        return resp;
    }
}

